/*
 * MainContainer.java
 *
 * Created on 10 Haziran 2008 Salı, 22:52
 */

package com.pegaa.uploader.ui;

import com.pegaa.uploader.common.StackLayout;
import com.pegaa.uploader.config.ConfigHolder;
import com.pegaa.uploader.ui.fileselection.FileSelectionContainer;
import com.pegaa.uploader.ui.selectedfilelist.SelectedFileListModel;
import com.pegaa.uploader.uploadui.UploadUIMainContainer;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;

/**
 *
 * @author  tayfun
 */
public class MainContainer extends javax.swing.JPanel implements ActionListener{
    
    private ConfigHolder configHolder = null;
    private StackLayout layout = null;
    private JPanel fileSelectorHolderPanel = null;
    private FileSelectionContainer fileSelection = null;
    private JPanel fileUploaderHolderPanel = null;
    private UploadUIMainContainer uploaderContainer = null;
    
    /** Creates new form MainContainer */
    public MainContainer() {
        initComponents();
    }
    
    public void setConfigHolder(ConfigHolder configHolder){
        this.configHolder = configHolder;
        this.updateComponents();
    }
    
    private void updateComponents()
    {
        this.layout = new StackLayout();
        this.setLayout(this.layout);
        
        this.createFileSelectorHolderPanel();
        this.createFileUploadHolderPanel();
        
       
        this.layout.showComponent(this.fileSelectorHolderPanel, this);
    }
    
    private void createFileSelectorHolderPanel()
    {
        this.fileSelectorHolderPanel = new JPanel();
        this.fileSelectorHolderPanel.setBackground(Color.BLUE);
        this.fileSelectorHolderPanel.setLayout(new GridLayout());
        
        this.fileSelection = new FileSelectionContainer();
        this.fileSelection.setConfigHolder(configHolder);
        this.fileSelectorHolderPanel.add(fileSelection);
        
        this.fileSelection.addActionListener(this);
        
        this.add(this.fileSelectorHolderPanel);
    }
    
    private void createFileUploadHolderPanel()
    {
        this.fileUploaderHolderPanel = new JPanel();
        this.fileUploaderHolderPanel.setLayout(new GridLayout());
        
        this.uploaderContainer = new UploadUIMainContainer();
        this.uploaderContainer.setConfigHolder(this.configHolder);
        this.fileUploaderHolderPanel.add(this.uploaderContainer);
        
        this.add(this.fileUploaderHolderPanel);
    }
    
    /**
     *  BottomToolBar event listener.
     * 
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("upload-button"))
        {
            SelectedFileListModel model = this.fileSelection.getModel();
            this.uploaderContainer.setModel(model);
            this.uploaderContainer.setTargetFolderID((String)e.getSource());
            
            this.layout.showComponent(fileUploaderHolderPanel, this);
            this.uploaderContainer.startUpload();
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(1)
            //.addGap(0, 598, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(1)
            //.addGap(0, 413, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
